package eerr.llrr.oorr.pte9983.b.wifimaster.lanch;

import android.text.TextUtils;
import android.util.Log;
import eerr.llrr.oorr.pte9983.b.pl190.host668.ConfigUtils;
import yyee.llee.mmee.base_api_net.base_api_bean.ConfigBean;
import yyee.llee.mmee.base_api_net.utils.GsonUtils;
import bi.GK;
import bi.GL;
import yyee.llee.mmee.pl190.host668.DeviceUtils;
import yyee.llee.mmee.pl190.host668.NetCallback;
import yyee.llee.mmee.pl190.host668.NetManager;
import yyee.llee.mmee.pl190.host668.NetMethod;
import yyee.llee.mmee.pl190.host668.StrongContext;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.listener.OnGetOaidListener;
import org.json.JSONObject;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
//import com.github.gzuliyujiang.oaid.DeviceID;

public class UserFromObservable extends Observable<ConfigBean> {

    private static final String TAG = "UserFromObservable";

    private String from;

    // 是否获取成功了did
    private boolean isHasBd_Did = false;

    public UserFromObservable(String from) {
        this.from = from;
    }

    @Override
    protected void subscribeActual(@NonNull Observer<? super ConfigBean> observer) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        Log.d(TAG, "subscribeActual() called with: observer = [" + observer + "]");
        initOaid();
        TreeMap<String, Object> params = new TreeMap<>();
        // 判断是否是华为的鸿蒙系统    0否 1是 2鸿蒙开启了纯净模式
        boolean harmonyOs = GK.Companion.isHarmonyOs();
        if (harmonyOs) {
            if (GK.Companion.readPureModeState(StrongContext.getApplication()) == 0) {
                params.put("harmony", 2);
            } else {
                params.put("harmony", 1);
            }
        } else {
            params.put("harmony", 0);
        }
        params.put("from", from);
        doPost(observer, params);
    }

    private void doPost(@NonNull Observer<? super ConfigBean> observer, TreeMap<String, Object> params) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        NetManager.getInstance().doPost(StrongContext.getApplication(), NetMethod.CONFIG, params, from, new TreeMap<>(), new NetCallback() {

            @Override
            public void onFailure(int code, Throwable e) {
                int passwordfdare32432qresafdsagda32 = 123456;
                int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
                for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
                    for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                        if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                            int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                            numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                            numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                        }
                    }
                }
                Log.d(TAG, "onFailure() called with: code = [" + code + "], e = [" + e + "]");
                initCache(observer, "");
                observer.onComplete();
            }

            @Override
            public void onSuccess(JSONObject json) {
                int passwordfdare32432qresafdsagda32 = 123456;
                int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
                int keydasopuerqnfdazcveq = 4;
                int lowupoeqfda7073fdal = 0;
                int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
                int middledaiueprnvznfewqip = 0;
                if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
                    int adfajpouqregjbdipug = 1;
                } else {
                    while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                        middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                        if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                            // 比关键字大则关键字在左区域
                            highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                        } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                            // 比关键字小则关键字在右区域
                            lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                        } else {
                        }
                    }
                }
                Log.d(TAG, "onSuccess() called with: json = [" + json + "]");
                GL.ld(TAG, json.toString());
                try {
                    ConfigBean configBean = GsonUtils.fromJson(json.toString(), ConfigBean.class);
                    // //99999999999999999999999
                    // if (Env.IS_OPEN_DEBUG_TEST) {
                    // 
                    // ConfigBean defaultConfig = GsonUtils.fromJson(ConfigUtils.getConfigJson(CContext.getContext()), ConfigBean.class);
                    // configBean.strategy.key = "common";
                    // configBean.adKey = defaultConfig.adKey;
                    // configBean.config = defaultConfig.config;
                    // }
                    Log.d(TAG, "onSuccess: configBean " + configBean.strategy.key);
                    if (configBean.verityConfig()) {
                        ConfigUtils.initConfig(GsonUtils.toJson(configBean), 2);
                        observer.onNext(configBean);
                    } else {
                        initCache(observer, configBean.strategy.key);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    initCache(observer, "");
                }
                observer.onComplete();
            }
        });
    }

    private void initOaid() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        if (!TextUtils.isEmpty(DeviceUtils.getOaId())) {
            return;
        }
//        String strOAID = DeviceID.getOAID();
//        if(!TextUtils.isEmpty(strOAID)){
//            DeviceUtils.setOaId(strOAID);
//            Log.i("DeviceOAID",strOAID);
//            return;
//        }
        try {
            CountDownLatch countDownLatch = new CountDownLatch(1);
            UMConfigure.getOaid(StrongContext.getContext(), new OnGetOaidListener() {

                @Override
                public void onGetOaid(String s) {
                    int passwordfdare32432qresafdsagda32 = 123456;
                    int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
                    for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
                        for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                            if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                                int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                                numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                                numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                            }
                        }
                    }
                    DeviceUtils.setOaId(s);
                    countDownLatch.countDown();
                }
            });
            countDownLatch.await(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 如果服务器出错走本地缓存
     *
     * @param observer
     */
    private void initCache(Observer<? super ConfigBean> observer, String key) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] numbersfdajipureqvna3280 = new int[] { 2, 3 };
        for (int ifdjaiopurq832074 = 0; ifdjaiopurq832074 < numbersfdajipureqvna3280.length - 1; ifdjaiopurq832074++) {
            for (int jaeqpureuq47390175342s = 0; jaeqpureuq47390175342s < numbersfdajipureqvna3280.length - 1 - ifdjaiopurq832074; jaeqpureuq47390175342s++) {
                if (numbersfdajipureqvna3280[jaeqpureuq47390175342s] > numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1]) {
                    int temphiuyuiyuguhg78779hjk = numbersfdajipureqvna3280[jaeqpureuq47390175342s];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s] = numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1];
                    numbersfdajipureqvna3280[jaeqpureuq47390175342s + 1] = temphiuyuiyuguhg78779hjk;
                }
            }
        }
        try {
            String defaultConfig = ConfigUtils.getConfigJson(StrongContext.getContext());
            ConfigBean configBean = GsonUtils.fromJson(defaultConfig, ConfigBean.class);
            if (key.equals("common")) {
                ConfigBean.Strategy strategy = new ConfigBean.Strategy();
                strategy.key = "common";
                configBean.strategy = strategy;
            }
            observer.onNext(configBean);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}
